package top.malaoshi.data;

import java.util.*;

/**
 * 索引管理器
 */
public class DataIndexManager {


    /**
     * key：索引列名 <br>
     * value：TreeMap类型  <br>
     *      key：索引值  <br>
     *      value：行号，List 类型，因为会出现多个索引值  <br>
     */
    private Map<String, TreeMap<Object,List<Integer>>> map = new HashMap<>();

    /**
     * 存放索引列名
     */
    private List<String> indexColumnNames = new ArrayList<>();
    protected DataIndexManager() {

    }

    /**
     * 是否包含索引
     * @param columnName
     * @return
     */
    public boolean isIndex(String columnName){
        return indexColumnNames.contains(columnName);
    }
    /**
     * 添加索引列名
     * @param column
     */
    public void addIndex(String column){
        indexColumnNames.add(column);
    }

    /**
     * 获取索引列名
     * @return
     */
    public String[] getIndexes(){
        String[] arr = new String[indexColumnNames.size()];
        indexColumnNames.toArray(arr);
        return arr;
    }


    /**
     * 保存索引值、行号
     * @param dr
     */
    protected void save(DataRow dr){
//        遍历索引列
        for(String column:indexColumnNames){
            // 取出索引值
            Object value = dr.getValue(column);
            if(value == null){// 值可能是null，直接跳过
                continue;
            }

            TreeMap<Object, List<Integer>> treeMap = map.get(column);
            if(treeMap == null){
                treeMap = new TreeMap<>();
                List<Integer> rowNumList = new ArrayList<>();
                rowNumList.add(dr.getRowid());
                treeMap.put(value,rowNumList);
                map.put(column,treeMap);
            }else{
                List<Integer> rowNumList = treeMap.get(value);
                if(rowNumList == null){
                    rowNumList = new ArrayList<>();
                    treeMap.put(value,rowNumList);
                }
                rowNumList.add(dr.getRowid());
            }
        }
    }
    /**
     * 根据列名、索引值获取行号
     * @param column
     * @param indexValue
     * @return 获取不到就返回null
     */
    protected List<Integer> getRowNumByIndex(String column,Object indexValue){
        TreeMap<Object, List<Integer>> treeMap = map.get(column);
        if(treeMap==null){
            return null;
        }
        List<Integer> list = treeMap.get(indexValue);
        return list;
    }


    /**
     * 根据索引列名获取索引值和行号
     * @param indexColumnName
     * @return
     */
    protected TreeMap<Object, List<Integer>> getIndexAndRowNum(String indexColumnName){
        TreeMap<Object, List<Integer>> treeMap = map.get(indexColumnName);
        return treeMap;
    }

//    清空索引
    protected void clear(){
        map.clear();
    }

    /**
     * 删除索引
     * @param dr
     */
    protected void remove(DataRow dr){
        for(String column:indexColumnNames){
            // 取出索引值
            Object value = dr.getValue(column);
            if(value == null){// 值可能是null，直接跳过
                continue;
            }

            TreeMap<Object, List<Integer>> treeMap = map.get(column);
            if(treeMap == null){

            }else{
                List<Integer> rowNumList = treeMap.get(value);
                if(rowNumList == null){

                }else{
                    Object rowid = dr.getRowid();
                    rowNumList.remove(rowid);
                }

            }
        }
    }

    /**
     * 保存索引值、行号
     * @param dr
     */
    protected void save(DataRow dr,String column){
        if(!indexColumnNames.contains(column)){
            return;
        }
        // 取出索引值
        Object value = dr.getValue(column);
        if(value == null){// 值可能是null，直接跳过
            return;
        }

        TreeMap<Object, List<Integer>> treeMap = map.get(column);
        if(treeMap == null){
            treeMap = new TreeMap<>();
            List<Integer> rowNumList = new ArrayList<>();
            rowNumList.add(dr.getRowid());
            treeMap.put(value,rowNumList);
            map.put(column,treeMap);
        }else{
            List<Integer> rowNumList = treeMap.get(value);
            if(rowNumList == null){
                rowNumList = new ArrayList<>();
                treeMap.put(value,rowNumList);
            }
            rowNumList.add(dr.getRowid());
        }

    }
    /**
     * 根据列名删除索引
     * @param dr
     */
    protected void remove(DataRow dr,String column){
        if(!indexColumnNames.contains(column)){
            return;
        }
        // 取出索引值
        Object value = dr.getValue(column);
        if(value == null){// 值可能是null，直接跳过
            return;
        }

        TreeMap<Object, List<Integer>> treeMap = map.get(column);
        if(treeMap == null){

        }else{
            List<Integer> rowNumList = treeMap.get(value);
            if(rowNumList == null){

            }else{
                Object rowid = dr.getRowid();
                rowNumList.remove(rowid);
            }

        }
    }
}
